<template>
  <bui-page-layout ref="pageLayout" :is-z-paging="false">
    <view class="member-benefit">
      <u-parse :content="pContent"></u-parse>
    </view>
    <template v-slot:footer>
      <b-button :loading="buttonLoading" v-if="!gStore.userInfo.levelValue" @change="wepayOrder" style="margin: 30rpx 40rpx">
        <text class="button_t">9.9元加入会员</text>
      </b-button>
    </template>
  </bui-page-layout>
</template>
<script setup>
import { ref, reactive, onMounted } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { globalStore } from "@/store";
const gStore = globalStore();
var status = ref("");
var buttonLoading = ref(false);
var pContent = ref("");
onLoad((options) => {
  status.value = options.type || "success";
});
const getMemberBenefit = () => {
  return new Promise((resolve, reject) => {
    uni.$request
      .service({
        url: "/cnt/memberBenefits",
        method: "POST",
        data: {},
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
const unifiedOrder = () => {
  return new Promise((resolve, reject) => {
    uni.$request
      .service({
        url: "/m/wx/unifiedOrder",
        method: "POST",
        data: {},
        isToast: true,
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
var wepayOrder = async () => {
  buttonLoading.value = true;
  var wepayInfo = await unifiedOrder();
  buttonLoading.value = false;
  WeixinJSBridge.invoke(
    "getBrandWCPayRequest",
    {
      appId: wepayInfo.appid,
      timeStamp: wepayInfo.timestamp,
      nonceStr: wepayInfo.nonceStr,
      package: "prepay_id=" + wepayInfo.prepay_id,
      signType: "MD5",
      paySign: wepayInfo.sign,
    },
    function (res) {
      if (res.err_msg == "get_brand_wcpay_request:ok") {
        gStore.getUserInfo();
        uni.switchTab({
          url: "/pages/user/index",
        });
      }
    }
  );
};
onMounted(async () => {
  var _getMemberBenefit = await getMemberBenefit();
  pContent.value = _getMemberBenefit.content;
});
</script>
<style lang="scss">
page {
  background-color: #f6f6f8;
}
.member-benefit {
  padding: 10rpx 30rpx 20rpx;
  line-height: 68rpx;
}
.submit {
  background-color: #fe9900;
  display: flex;
  justify-content: center;
  border-radius: 16rpx;
  padding: 22rpx 0;
  margin: 30rpx 40rpx;
  box-shadow: 0px 0px 40rpx 0px rgba(0, 0, 0, 0.15);
  .t {
    font-size: 32rpx;
    color: #fff;
  }
}
</style>
